package net.homelinux.chaoswg.io.renga.krypto.animations.Pht;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

import net.homelinux.chaoswg.io.renga.core.animations.Animation;
import net.homelinux.chaoswg.io.renga.core.animations.State;

public class PhtAnimation extends Animation {
    PhtState initialState;
    PhtStateChanger phtChanger;
    
    JLabel firstOperandsDisplay;
    JLabel secondOperandsDisplay;
    JLabel firstResultDisplay;
    JLabel secondResultDisplay;
    JWindow jw;
    
    public PhtAnimation () {
        initialize(new PhtState());
    }

    public PhtAnimation (PhtState state) {
        initialize(state);
        
        jw = new JWindow ();
        jw.getContentPane().add(getPanel());
        jw.setLocationRelativeTo(null);
        
        jw.setSize(100, 40);
    }
    
    public JPanel getPanel() {
        JPanel toReturn = new JPanel (new GridLayout (4,1));
        
        toReturn.add(firstOperandsDisplay = new JLabel (initialState.getOperand0().toString()));
        toReturn.add(secondOperandsDisplay = new JLabel (initialState.getOperand1().toString()));
        toReturn.add(firstResultDisplay = new JLabel (initialState.getResult0().toString()));
        toReturn.add(secondOperandsDisplay = new JLabel (initialState.getResult1().toString()));
        
        return toReturn;
    }

    private void initialize(State initialState) {
        if (!(initialState instanceof PhtState)) {
            throw new IllegalArgumentException ();
        }
        this.initialState = ((PhtState)initialState);
        this.phtChanger = new PhtStateChanger (this.initialState);
    }

    @Override
    public void start() {
        Thread t = new Thread (phtChanger);
        t.setDaemon(true);
        t.start();
        
        try {
            t.join();
        } catch (InterruptedException iexcp){ 
            iexcp.printStackTrace();
        }
        phtChanger.close ();
        finished = true;
        setChanged();
        notifyObservers();
    }
    
    public void setOperand(int i, Object operand) {
        initialState.setOperand(i, operand);
    }

}
